<template>
  <div>
    <el-card class="box-card" style="margin: 20px 0">
      <CategorySelect :show="!isShowTable" @getCategoryId="getCategoryId"></CategorySelect>
    </el-card>
    <el-card class="box-card">
      <div v-show="isShowTable">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addAttr"
          :disabled="!category3Id"
          >添加属性</el-button
        >
        <el-table border style="width: 100%" :data="attrList">
          <el-table-column label="序号" type="index" width="80" align="center">
          </el-table-column>
          <el-table-column label="属性名称" prop="attrName" width="150">
          </el-table-column>
          <el-table-column label="属性值列表" prop="attrValueList">
            <template slot-scope="{ row, $index }">
              <el-tag
                type="success"
                style="margin: 0 20px"
                v-for="(attr, index) in row.attrValueList"
                >{{ attr.valueName }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="{ row, $index }">
              <el-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                @click="updateAttr(row)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 修改或添加属性 -->
      <div v-show="!isShowTable">
        <el-form ref="form" label-width="80px" border :model="attrInfoList">
          <el-form-item label="属性名">
            <el-input
              v-model="attrInfoList.attrName"
              placeholder="请输入内容"
              style="width: 200px"
            ></el-input>
          </el-form-item>
        </el-form>

        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!attrInfoList.attrName"
          @click="addAttrValue"
          >添加属性值</el-button
        >
        <el-button @click="isShowTable = true">取消</el-button>

        <el-table border style="width: 100%" :data="attrInfoList.attrValueList">
          <el-table-column type="index" label="序号" width="80">
          </el-table-column>
          <el-table-column label="属性值名称" width="width">
            <template slot-scope="{ row, $index }">
              <el-input
                :ref="$index"
                v-if="row.falg"
                v-model="row.valueName"
                placeholder="请输入属性值名称"
                size="mini"
                @blur="toView(row)"
                @keyup.native.enter="toView(row)"
              ></el-input>
              <span
                @click="toEdit(row, $index)"
                v-else
                style="display: block"
                >{{ row.valueName }}</span
              >
            </template>
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-popconfirm :title="`确定删除${row.valueName}?`" @onConfirm="deleteAttr($index)" >
                <el-button
                  slot="reference"
                  type="danger"
                  icon="el-icon-delete"
                  
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <el-button type="primary" :disabled="isDisabled" @click="saveAttr"
          >保存</el-button
        >
        <el-button @click="isShowTable = true">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import _ from "lodash";
export default {
  name: "Attr",
  data() {
    return {
      //三级分类的id
      category1Id: "",
      category2Id: "",
      category3Id: "",
      //商品基础属性数据
      attrList: [],
      isShowTable: true,
      attrInfoList: {
        attrName: "", //属性名
        attrValueList: [
          //属性值
        ],
        categoryId: 0, //三级分类id
        categoryLevel: 0, //区别几级分类id
      },
    };
  },
  computed: {
    isDisabled() {
      if (this.attrInfoList.attrValueList.length === 0) {
        return true;
      } else {
        return false;
      }
    },
  },
  methods: {
    //获取id
    getCategoryId(categoryData) {
      if (categoryData.level === 1) {
        this.category1Id = categoryData.id;
      } else if (categoryData.level === 2) {
        this.category2Id = categoryData.id;
      } else {
        this.category3Id = categoryData.id;
        this.getAttrList();
      }
    },
    //获取商品基础属性
    async getAttrList() {
      const { category1Id, category2Id, category3Id } = this;
      let reslut = await this.$API.attr.reqAttrInfoList(
        category1Id,
        category2Id,
        category3Id
      );
      if (reslut.code === 200) {
        this.attrList = reslut.data;
        console.log(reslut);
      } else {
        // this.$message.error(reslut.message);
        this.$message.error(reslut.data)
      }
    },
    //添加属性
    addAttr() {
      this.isShowTable = false;
      this.attrInfoList = {
        attrName: "", //属性名
        attrValueList: [
          //属性值
        ],
        categoryId: this.category3Id, //三级分类id
        categoryLevel: 3, //区别几级分类id
      };

    },
    //添加属性值
    addAttrValue() {
      this.attrInfoList.attrValueList.push({
        attrId: this.attrInfoList.id, //相应的属性名id
        valueName: "",
        falg: true,
      });
      this.$nextTick(() => {
        this.$refs[this.attrInfoList.attrValueList.length - 1].focus();
      });
    },
    //保存
    async saveAttr() {
      this.attrInfoList.attrValueList=this.attrInfoList.attrValueList.filter(item=>{
        //过滤掉属性值不是空的
        if(item.valueName!=''){
            //删除掉flag属性
            delete item.flag;
            return true;
          }
      })

      let result = await this.$API.attr.addOrUpdateAttrInfoList(this.attrInfoList);
      console.log(result);
        try {
          this.$message.success(result.message);
          this.isShowTable = true;
          this.getAttrList()
        } catch (error) {
          // this.$message.error(result.message)
          this.$message.error(result.data)
        }
        
    },
    //删除添加的属性
    deleteAttr(i) {
      console.log(123123);
      this.attrInfoList.attrValueList.splice(i, 1);
    },
    //修改
    updateAttr(row) {
      this.isShowTable = false;
      this.attrInfoList = _.cloneDeep(row);

      this.attrInfoList.attrValueList.forEach((item) => {
        this.$set(item, "falg", false);
      });
    },
    //转变为视图模式
    toView(row) {
      //输入的属性值不能为空和空格
      if (row.valueName.trim() !== "") {
        console.log(row.valueName.trim());
        row.falg = false;
      } else {
        this.$message("请你输入一个正常的属性值！");
        return;
      }
      //属性值不能重复
      let isRepeat = this.attrInfoList.attrValueList.some((item) => {
        //row需要从this.attrInfoList.attrValueList去除
        if (row !== item) {
          return row.valueName === item.valueName;
        }
      });
      console.log(isRepeat);

      if (isRepeat) {
        this.$message.error("属性值不能重复");
        this.attrInfoList.attrValueList.splice(-1, 1);
        return;
      }
    },
    //编辑模式
    toEdit(row, index) {
      row.falg = true;

      this.$nextTick(() => {
        this.$refs[index].focus();
      });
    },
  },
};
</script>
